package com.example.chapter07_client.util;

import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.content.ContextCompat;

public class PermissionUtil {
    // 检查多个权限，返回 true 表示已完全启用权限，返回 false 表示未完全启用权限
    public static boolean checkPermission(Activity activity, String[] permissions, int requestCode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int check_permission = PackageManager.PERMISSION_GRANTED;
            for (String permission : permissions) {
                check_permission = ContextCompat.checkSelfPermission(activity, permission);
                if (check_permission != PackageManager.PERMISSION_GRANTED) {
                    break;
                }
            }
            // 未完全启用权限，则请求系统弹框，好让用户手动授权
            if (check_permission != PackageManager.PERMISSION_GRANTED) {
                activity.requestPermissions(permissions, requestCode);
                return false;
            }
        }
        return true;
    }

    // 检查权限请求结果，返回 true 表示已完全启用权限，返回 false 表示至少有一个权限未启用
    public static boolean checkGrant(int[] grantResults) {
        if (grantResults != null) {
            for (int grantResult : grantResults) {
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }



}
